using System;
using Jink.Core.Converters.Annotations;
using Jink.Core.Json.Values;
using XenoGears.Assertions;

namespace Jink.Core.Json.Converters.Scalars
{
    [Converter]
    internal static class ScalarDeserializers
    {
        [Converter(Weight = 0.5)]
        public static int IntFromJson(JsonExpression jex)
        {
            return (int)jex.Value;
        }

        [Converter(Weight = 0.5)]
        public static double DoubleFromJson(JsonExpression jex)
        {
            return (double)jex.Value;
        }

        [Converter(Weight = 0.5)]
        public static String StringFromJson(JsonExpression jex)
        {
            return (String)jex.Value;
        }

        [Converter(Weight = 0.5)]
        public static bool BoolFromJson(JsonExpression jex)
        {
            return (bool)jex.Value;
        }

        [Converter(Weight = 0.5)]
        public static DateTime DateTimeFromJson(JsonExpression dateTime)
        {
            throw new NotImplementedException();
        }

        [Converter(Weight = 0.5)]
        public static Guid GuidFromJson(JsonExpression jex)
        {
            return new Guid(jex.Value.AssertCast<String>());
        }

        [Converter(Weight = 0.5)]
        public static T? NullableFromJson<T>(JsonValue json)
            where T : struct
        {
            if (json == new JsonExpression("null"))
            {
                return null;
            }
            else
            {
                return json.FromJson<T>();
            }
        }
    }
}